﻿using MediatR;
using W.Device.API.Application.Command;
using W.Device.Repository.Interface;

namespace W.Device.API.Application.CommandHandlder
{
    public class DeviceUpdateAllCommandHandler : IRequestHandler<DeviceUpdateAllCommand, int>
    {
        private readonly IDeviceRepository deviceRepository;

        public DeviceUpdateAllCommandHandler(IDeviceRepository deviceRepository)
        {
            this.deviceRepository = deviceRepository;
        }

        public async Task<int> Handle(DeviceUpdateAllCommand request, CancellationToken cancellationToken)
        {
            var list = (await deviceRepository.GetAllAsaync()).Where(w => request.ints.Contains(w.DeviceId)).ToList();
            list.ForEach(w =>
            {
                w.DeviceAccreNo = request.DeviceAccreNo;
                w.DeviceNo = request.DeviceNo;
                w.DeviceName = request.DeviceName;
                w.IsAccre = request.IsAccre;
                w.DeviceStatus = request.DeviceStatus;
                w.DeviceRemarks = request.DeviceRemarks;
                w.UpdateTime = request.UpdateTime;
            });
            return await deviceRepository.UpdateAll(list);
        }
    }
}
